package com.swu.pad.comment.service.impl;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.mail.MessagingException;

import org.hibernate.boot.internal.IdGeneratorInterpreterImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.swu.pad.assignment.dao.AssignmentDao;
import com.swu.pad.assignment.dao.AssignmentPadDao;
import com.swu.pad.assignment.entity.Assignment;
import com.swu.pad.assignment.entity.AssignmentPad;
import com.swu.pad.comment.dao.CommentDao;
import com.swu.pad.comment.dao.CommentQuestionDao;
import com.swu.pad.comment.entity.Comment;
import com.swu.pad.comment.entity.CommentQuestion;
import com.swu.pad.comment.service.CommentService;
import com.swu.pad.course.dao.CourseDao;
import com.swu.pad.course.dao.CourseGroupUserDao;
import com.swu.pad.course.dao.GroupDao;
import com.swu.pad.course.entity.CourseGroupUser;
import com.swu.pad.course.entity.Group;
import com.swu.pad.system.dao.UserDao;
import com.swu.pad.system.entity.User;
import com.swu.pad.util.MailUtil;


@Service
@Transactional
public class CommentServiceImpl implements CommentService {

	@Autowired
	private CommentDao commentDao;
	@Autowired
	private GroupDao groupDao;
	@Autowired
	private AssignmentPadDao assignmentPadDao;
	@Autowired
	private CourseGroupUserDao courseGroupUserDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private AssignmentDao assignmentDao;
	@Autowired
	private CommentQuestionDao commentQuestionDao;
	@Autowired
	private CourseDao courseDao;
	
	


	public List<Comment> getComment(String assignmentPadId) {
		return commentDao.findByAssignmentPadId(assignmentPadId);
	}

	public Comment saveOrUpateComment(Comment comment) {
		Date date=new Date();
	    comment.setCommentTime(date);
	    
	  
	    
		String mutualTemplateId;
		String groupid;
		AssignmentPad assignmentPad;
		List<CommentQuestion> questions;
		assignmentPad=assignmentPadDao.findOne(comment.getAssignmentPadId());
		List<CourseGroupUser> groupUsers; 
		String commenterName;
		String courseId;
		String courseName;
		String assignmentName;
		User commenter=userDao.findOne(comment.getCommenterId());
//		User author=userDao.findOne(comment.getAuthorId());//被打分的人
		commenterName=commenter.getRealname();//评价打分者的名字
		//由于前台 循环调用保存，所以要判断只有根据本次 组内评价   在进行发送邮件
		Assignment assignment=assignmentDao.findOne(assignmentPad.getAssignmentId());
		mutualTemplateId=assignment.getMutualTemplateId();//拿到本次作业互评模板id
		courseId=assignment.getCourseId();
		courseName=courseDao.findOne(courseId).getCourseName();//课程名称
		assignmentName=assignment.getAssignmentName();//作业名称
		questions=commentQuestionDao.findByTemplateId(mutualTemplateId);
		
		//每次在模板id 等于第一个模板id时发送邮件
		if(comment.getQuestionId().equals(questions.get(0).getId())){
			  //在这里向全组人员发一个邮件通知他们组内成员进行了组内评价start
			   //1 根据作业 assignmentpad_id 查询获得  groupid
			
			   groupid=assignmentPad.getGroupId();
			   //2 根据小groupid 获得groupuser
			   groupUsers=courseGroupUserDao.findCourseGroupUserByGroupId(groupid);
			 
			   //3 根据groupuser遍历发送邮件 ， 只有 commenterid ！= 组成员id时才发送邮件
			   for(int i=0;i<groupUsers.size();i++){
				     
					   if(groupUsers.get(i).getUserId().equals(comment.getAuthorId())){//找到被评价的人给他发一封邮件
						 int count=0;
					     List<Comment> authorComments=commentDao.findByAssignmentPadIdAndAuthorId(comment.getAssignmentPadId(),comment.getAuthorId());
					    
					     for (int j = 0; j < groupUsers.size(); j++) {
							for(int k=0;k<authorComments.size();k++){
							  if(groupUsers.get(j).getUserId().equals(authorComments.get(k).getCommenterId())){
								  count++;
								  break;
							  }
							}
						 }
					     
					     User user=userDao.findUserById(comment.getAuthorId());//被评价的人
					     
					     if(count>=(groupUsers.size()-2)){//说明打分人数已经达到 小组人数减一   发送建议 邮件
					      try {
					    	  //System.out.println("人数达到上限，发送建议邮件");
							//MailUtil.sendMailToGroupUser(user.getEmail(),user.getRealname(),courseName,assignmentName);
						} 
						catch (Exception e) {
							//System.out.println("评价人数达到上限，发送邮件失败！");
							e.printStackTrace();
						}
					    	 
					     } else {//评论人数 不够  多，只提醒他有人给他打分了   如果评论者已经给过评论   ，可以不再 重复发送
					    	 try {  
					    		    String noMoreMailCommenterId=comment.getCommenterId();
					    		    List<Comment> noMoreList=commentDao.findByAssignmentPadIdAndAuthorIdAndCommenterId(comment.getAssignmentPadId(),comment.getAuthorId(),comment.getCommenterId());
					    		    if(noMoreList.size()<1){
//					    		    System.out.println("人数没有达到上限，发送提醒邮件");
//									MailUtil.sendMailToGroupUserLess(user.getEmail(),user.getRealname(), courseName, assignmentName);
					    		    }
								} 
								catch (Exception e) {
									//System.out.println("评价人数没有达到上限，发送邮件失败！");
									e.printStackTrace();
						}
					   
					   }
			
		}
		
		}
		}   
			  return  commentDao.saveAndFlush(comment);
			  
		   
		}
		
	

	public void deleteComment(String commentId) {
		commentDao.delete(commentId);
	}

}
